<template>
  <div class="detailbox">
    <div class="flexbox">
      <div class="shrinkbox_v2">
        企业名称
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.enterpriseName" placeholder="请输入企业名称"></el-input>
        <div v-else class="div_input">
          {{ editData.enterpriseName }}
        </div>
      </div>
      <div class="shrinkbox_v2">
        企业全名
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.enterpriseFullName" placeholder="请输入企业全名"></el-input>
        <div v-else class="div_input">
          {{ editData.enterpriseFullName }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        企业代码
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.enterpriseCode" placeholder="请输入企业代码"></el-input>
        <div v-else class="div_input">
          {{ editData.enterpriseCode }}
        </div>
      </div>
      <div class="shrinkbox_v2">
        企业过期时间
      </div>
      <div class="contentbox_v2 diydatapicker">
        <div class="div_input" v-if="optype == 'add'">10天</div>
        <div style="display: flex;align-items: center;" v-else>
          <div class="div_input" style="flex:1;">
            剩余{{editData.remainDays}}天
          </div>
          <div style="margin-right: 10px;">
            <el-button @click.stop="setMoreDate()" type="primary" size="small">延期10天</el-button>
          </div>
        </div>
        <!-- <el-date-picker v-if="!feildConfig.isReadonly"
          v-model="editData.expireTime"
          type="date"
          placeholder="选择日期"/>
        <div v-else class="div_input">
          {{ editData.expireTime }}
        </div> -->
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        企业地址
      </div>
      <div class="contentbox_v2">
        <div style="display: flex;" v-if="!feildConfig.isReadonly" class="diyselect">
          <el-select @change="changeprovince" filterable v-model="editData.province" class="diyselect" placeholder="请选择省" style="width:250px;">
            <el-option
              v-for="item in provincedatas"
              :key="item.code"
              :label="item.value"
              :value="item.code"
            />
          </el-select>
          <el-select @change="changecity" filterable v-model="editData.city" class="diyselect" placeholder="请选择市" style="width:250px;">
            <el-option
              v-for="item in citydatas"
              :key="item.code"
              :label="item.value"
              :value="item.code"
            />
          </el-select>
          <el-select @change="changedistrict" filterable v-model="editData.district" class="diyselect" placeholder="请选择区" style="width:250px;">
            <el-option
              v-for="item in districtdatas"
              :key="item.code"
              :label="item.value"
              :value="item.code"
            />
          </el-select>
          <el-input class="diyinput" v-model="editData.address" placeholder="请输入详细地址"/>
        </div>
        <div v-else>
          {{ editData.provinceName }}
          {{ editData.cityName }}
          {{ editData.districtName }}
          {{ editData.addressName }}
        </div>
      </div>
    </div>
    
    <!-- <div class="flexbox">
      <div class="shrinkbox_v2">
        企业状态
      </div>
      <div class="contentbox_v2">
        <el-select v-if="!feildConfig.isReadonly" v-model="editData.enterpriseState" class="diyselect" placeholder="请选择企业状态">
          <el-option label="正式企业" value="1" />
          <el-option label="试用企业" value="2" />
        </el-select>
        <div v-else class="div_input">
          {{ editData.enterpriseState == '1'?'正式企业':'试用企业' }}
        </div>
      </div>
    </div> -->
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        企业注册手机号
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" :maxlength="11" class="diyinput" v-model="editData.enterpriseMobile" placeholder="请输入企业注册手机号"></el-input>
        <div v-else class="div_input">
          {{ editData.enterpriseMobile }}
        </div>
      </div>
      <div class="shrinkbox_v2">
        验证码
      </div>
      <div class="contentbox_v2">
        <el-input
          class="diyinput"
          :placeholder="'请输入验证码'"
          v-model="editData.verifyCode"
          autocomplete="new-password">
          <template #append>
            <span @click.stop="timmers.cansend?sendSMS():''" class="no_select" style="color: #409EFF;cursor: pointer;">
              {{ sendMsg }}
            </span>
          </template>
        </el-input>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        最大账户数量
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.maxNum" placeholder="请输入最大账户数量"></el-input>
        <div v-else class="div_input">
          {{ editData.maxNum }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        是否开启配比
      </div>
      <div class="contentbox_v2">
        <el-select v-if="!feildConfig.isReadonly" v-model="editData.isUsePB" class="diyselect" placeholder="请选择状态">
          <el-option :value="true" label="开启配比"/>
          <el-option :value="false" label="不开启配比"/>
        </el-select>
        <div v-else class="div_input">
          {{ editData.isUsePB?'开启配比':'不开启配比' }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        Erp名称
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.erpName" placeholder="请输入Erp名称"></el-input>
        <div v-else class="div_input">
          {{ editData.erpName }}
        </div>
      </div>
      <div class="shrinkbox_v2">
        是否绑定ERP
      </div>
      <div class="contentbox_v2">
        <el-select v-if="!feildConfig.isReadonly" v-model="editData.isERP" class="diyselect" placeholder="请选择状态">
          <el-option :value="true" label="绑定ERP"/>
          <el-option :value="false" label="不绑定ERP"/>
        </el-select>
        <div v-else class="div_input">
          {{ editData.isERP?'绑定ERP':'不绑定ERP' }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        Erp域名
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.erpBasePath" placeholder="请输入Erp域名"></el-input>
        <div v-else class="div_input">
          {{ editData.erpBasePath }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        配比运算Erp
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.erpPath" placeholder="请输入配比运算Erp地址"></el-input>
        <div v-else class="div_input">
          {{ editData.erpPath }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        订单导入Erp
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" v-model="editData.erpOrderPath" placeholder="请输入订单导入Erp地址"></el-input>
        <div v-else class="div_input">
          {{ editData.erpOrderPath }}
        </div>
      </div>
    </div>
    
    <div class="flexbox">
      <div class="shrinkbox_v2">
        备注
      </div>
      <div class="contentbox_v2">
        <el-input v-if="!feildConfig.isReadonly" class="diyinput" type="textarea" v-model="editData.comments" placeholder="请输入备注"></el-input>
        <div v-else class="div_input">
          {{ editData.comments }}
        </div>
      </div>
    </div>



    

  </div>
</template>

<script setup>
  import areas from '@/../public/static/json/areadatas.json'
  import { ref, inject, defineEmits } from 'vue'
  // import { useStore } from 'vuex'
  // 注入全局utils工具js
  const $utils = inject('$utils')
  const editData = ref({})
  const feildConfig = ref({
    isReadonly: false
  })
  // 子传父
  const emit = defineEmits([])

  // 省份信息
  const provincedatas = ref(areas)
  // 城市信息
  const citydatas = ref([])
  // 区域信息
  const districtdatas = ref([])

  const timmers = ref({
    cansend: true,
    smsTimmer: null,
    time_num: 60
  })
  const sendMsg = ref('发送')

  const optype = ref('')
  

  function initData(obj) {
    optype.value = obj.type
    editData.value = obj.data ? obj.data : {}
    feildConfig.value = obj.feildConfig ? obj.feildConfig : feildConfig.value
  }
  // 选择省事件
  function changeprovince(e, flag) {
    const index = provincedatas.value.findIndex((item) => {return parseInt(item.code) == parseInt(e)})
    citydatas.value = provincedatas.value[index].children
    editData.value.provinceName = provincedatas.value[index].value
    if (!flag) {
      editData.value.city = ''
      editData.value.cityName = ''
      editData.value.district = ''
      editData.value.districtName = ''
    }
  }
  // 选择市事件
  function changecity(e, flag) {
    const index = citydatas.value.findIndex((item) => {return parseInt(item.code) == parseInt(e)})
    districtdatas.value = citydatas.value[index].children
    editData.value.cityName = citydatas.value[index].value
    if (!flag) {
      editData.value.district = ''
      editData.value.districtName = ''
    } 
  }
  // 选择区事件
  function changedistrict(e, flag) {
    const index = districtdatas.value.findIndex((item) => {return parseInt(item.code) == parseInt(e)})
    editData.value.districtName = districtdatas.value[index].value
  }

  function sendSMS () {
    if (!editData.value.enterpriseMobile ) {
      $utils.message({
        message: '请输入手机号码',
        type: 'error'
      })
    } else {
      if (!timmers.value.smsTimmer) {
        clearInterval(timmers.value.smsTimmer)
      }
      timmers.value.cansend = false
      timmers.value.smsTimmer = setInterval(() => {
        if (--timmers.value.time_num == 0) {
          timmers.value.time_num = 60
          timmers.value.cansend = true
          sendMsg.value = '发送'
          clearInterval(timmers.value.smsTimmer)
        } else {
          sendMsg.value = `${timmers.value.time_num}S`
        }
      }, 1000)
      $utils.post({
        url: '/Api/Enterprise/SendSMS',
        params: {
          phone: editData.value.enterpriseMobile,
          useType: 'Register'
        },
        // showLoading: true,
        ok: function (res) {
          
        }
      })
    }
  }


  function setMoreDate () {
    $utils.post({
      url: '/Api/Enterprise/Extension',
      params: {
        id: editData.value.id
      },
      showLoading: true,
      confirm: true,
      confirmText: '是否延长10天使用时间？',
      ok: function (res) {
        $utils.message({
          message: '成功延长【10】天使用时间',
          type: 'success'
        })
        editData.value.remainDays = parseInt(editData.value.remainDays) + 10
        emit('refreshData')
      }
    })
  }

  




  function submitData() {
    if (!!editData.value.expireTime) {
      editData.value.expireTime = new Date(editData.value.expireTime).toDateString('chinese', {hour12: false})
    }
    $utils.post({
      url: '/Api/Enterprise/DoSave',
      params: editData.value,
      showLoading: true,
      ok: function (res) {
        emit('close-dialog', false)
      }
    })
  }

  defineExpose({
    initData,
    submitData
  })

</script>

<style scoped>
.shrinkbox_v2 {
  width: 90px !important;
}
.saveBtn {
  background-color: #4999FA;
  color: #fff;
  font-weight: normal;
  font-size: 14px;
  padding: 3px 10px;
  border-radius: 3px;
  margin-right: 10px;
}
.saveBtn:hover {
  /* background-color: #7BB8FB; */
  opacity: 0.8;
  cursor: pointer;
}
.split_title {
  background-color: #EBEEF5;
  font-size: 16px;
  height: 30px;
  align-items: center;
  font-family: '楷体';
  font-weight: bold;
  display: flex;
  position: relative;
}
.split_title_content {
  flex-grow: 1;
  text-align: center;
}
.split_title_btn {
  display: flex;
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  align-items: center;
}
.shrinkbox_v2 {
  width: 70px;
}
.diyinput :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}

.diyselect :deep(.el-select__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diyselect :deep(.el-select__wrapper:hover){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyselect :deep(.el-select__wrapper:active){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
/* .diyselect :deep(.el-input__wrapper:hover){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyselect :deep(.el-input__wrapper:active){
  border: 0 !important;
  outline: none;
  box-shadow: none;
} */
.diyinput :deep(.el-textarea__inner){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diydatepicker :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diyformitem {
  margin-bottom: 0;
}
.diydatapicker :deep(.el-date-editor){
  width: 100%;
}
.diydatapicker :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyinput :deep(.el-input-group__append){
  border: 0 !important;
  outline: none;
  background-color: unset;
  box-shadow: none;
  padding: 0 10px 0 5px;
  font-weight: bold;
  font-size: 16px;
}
/deep/ input::-webkit-outer-spin-button,
/deep/ input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/ input[type='number'] {
  -moz-appearance: textfield;
}
</style>